package com.wenbo.fish.utils;

import cn.hutool.http.HttpRequest;
import org.apache.commons.collections4.MapUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @author changwenbo
 * @date 2024/8/23 17:42
 */
public final class HttpUtils {
    private HttpUtils() {}

    private static final int TIME_OUT = 30 * 1000;

    private static final String UA = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36";
    private static final String REFERER = "https://www.baidu.com";

    public static String get(String url) {
        return HttpRequest.get(url).timeout(TIME_OUT).execute().body();
    }

    public static String get(String url, Map<String, String> headers) {
        if (MapUtils.isEmpty(headers)) {
            headers = new HashMap<>();
        }
        if (headers.containsKey("User-Agent")) {
            headers.put("User-Agent", UA);
        }
        if (headers.containsKey("Referer")) {
            headers.put("Referer", REFERER);
        }

        return HttpRequest.get(url).timeout(TIME_OUT).addHeaders(headers).execute().body();
    }
}
